<html>
<head><meta charset="utf-8"><title>Extern static references · t-lang/wg-unsafe-code-guidelines · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/index.html">t-lang/wg-unsafe-code-guidelines</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Extern.20static.20references.html">Extern static references</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="180364303"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Extern%20static%20references/near/180364303" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> DutchGhost <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Extern.20static.20references.html#180364303">(Nov 10 2019 at 13:54)</a>:</h4>
<p>Hi, I was wondering if it is UB to have references that are 0 in external statics:</p>
<div class="codehilite"><pre><span></span>extern {
    static V: &amp;&#39;static u8;
}

mod internal {
    #[no_mangle]
    static V: usize = 0;
}

fn main() {
    println!(&quot;{:p}&quot;, unsafe { V });
}
</pre></div>


<p>That program wil print <code>0x00</code>. Now, you might say I had to use <code>unsafe</code> to access the external static, but having invalid references is UB by definition, and I didn't use any unsafe to do that.</p>



<a name="180364638"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Extern%20static%20references/near/180364638" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> DutchGhost <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Extern.20static.20references.html#180364638">(Nov 10 2019 at 14:04)</a>:</h4>
<p>Oh, related issue seems <a href="https://github.com/rust-lang/rust/issues/28179" target="_blank" title="https://github.com/rust-lang/rust/issues/28179">https://github.com/rust-lang/rust/issues/28179</a> . Should I add this example there?</p>



<a name="180365272"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Extern%20static%20references/near/180365272" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Extern.20static.20references.html#180365272">(Nov 10 2019 at 14:25)</a>:</h4>
<p>no_mangle can often sort of "sneak in" unsafe, since you're essentially overriding an arbitrary symbol</p>



<a name="180365319"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Extern%20static%20references/near/180365319" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Extern.20static.20references.html#180365319">(Nov 10 2019 at 14:26)</a>:</h4>
<p>but in general, yes, I'd say that program has UB.</p>



<a name="180365949"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Extern%20static%20references/near/180365949" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Extern.20static.20references.html#180365949">(Nov 10 2019 at 14:43)</a>:</h4>
<p>that's instant UB, and you don't need <code>#[no_mangle]</code> for it</p>



<a name="180366007"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Extern%20static%20references/near/180366007" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Extern.20static.20references.html#180366007">(Nov 10 2019 at 14:44)</a>:</h4>
<div class="codehilite"><pre><span></span><span class="k">extern</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="k">static</span><span class="w"> </span><span class="n">V</span>: <span class="kp">&amp;</span><span class="nb">&#39;static</span><span class="w"> </span><span class="kt">u8</span><span class="p">;</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
<span class="k">fn</span> <span class="nf">main</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="p">}</span><span class="w"></span>
</pre></div>



<a name="180366013"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Extern%20static%20references/near/180366013" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Extern.20static.20references.html#180366013">(Nov 10 2019 at 14:44)</a>:</h4>
<p>If <code>V</code> is ever null, that program has UB</p>



<a name="180366020"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Extern%20static%20references/near/180366020" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> DutchGhost <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Extern.20static.20references.html#180366020">(Nov 10 2019 at 14:44)</a>:</h4>
<blockquote>
<p>that's instant UB, and you don't need <code>#[no_mangle]</code> for it</p>
</blockquote>
<p>Is it insta UB <em>without</em> accessing the static?</p>



<a name="180366034"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Extern%20static%20references/near/180366034" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Extern.20static.20references.html#180366034">(Nov 10 2019 at 14:45)</a>:</h4>
<p>Yes, just constructing the static with an invalid value is UB</p>



<a name="180366041"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Extern%20static%20references/near/180366041" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Extern.20static.20references.html#180366041">(Nov 10 2019 at 14:45)</a>:</h4>
<p>The access happens somewhere on program initialization, when a value is assigned to the static "somehow"</p>



<a name="180366094"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Extern%20static%20references/near/180366094" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> DutchGhost <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Extern.20static.20references.html#180366094">(Nov 10 2019 at 14:46)</a>:</h4>
<blockquote>
<p>Yes, just constructing the static with an invalid value is UB</p>
</blockquote>
<p>Is that defined anywere in the reference? Or some place otherwise?</p>



<a name="180366099"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Extern%20static%20references/near/180366099" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Extern.20static.20references.html#180366099">(Nov 10 2019 at 14:46)</a>:</h4>
<p>In the unsafe code guidelines, that's the validity invariant</p>



<a name="180366118"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Extern%20static%20references/near/180366118" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Extern.20static.20references.html#180366118">(Nov 10 2019 at 14:46)</a>:</h4>
<p>See here: <a href="https://github.com/rust-lang/unsafe-code-guidelines/blob/master/reference/src/glossary.md#validity-and-safety-invariant" target="_blank" title="https://github.com/rust-lang/unsafe-code-guidelines/blob/master/reference/src/glossary.md#validity-and-safety-invariant">https://github.com/rust-lang/unsafe-code-guidelines/blob/master/reference/src/glossary.md#validity-and-safety-invariant</a></p>



<a name="180366138"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Extern%20static%20references/near/180366138" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Extern.20static.20references.html#180366138">(Nov 10 2019 at 14:47)</a>:</h4>
<p>It's the same kind of UB as a <code>let x: bool = transmute(42);</code></p>



<a name="180366145"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Extern%20static%20references/near/180366145" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Extern.20static.20references.html#180366145">(Nov 10 2019 at 14:47)</a>:</h4>
<p>even if you never access the <code>x</code> afterwards, just creating the <code>bool</code> with the invalid value is already UB</p>



<a name="180366157"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Extern%20static%20references/near/180366157" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> DutchGhost <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Extern.20static.20references.html#180366157">(Nov 10 2019 at 14:47)</a>:</h4>
<blockquote>
<p>even if you never access the <code>x</code> afterwards, just creating the <code>bool</code> with the invalid value is already UB</p>
</blockquote>
<p>But the difference is that for the transmute, you need to write <code>unsafe</code></p>



<a name="180366213"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Extern%20static%20references/near/180366213" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Extern.20static.20references.html#180366213">(Nov 10 2019 at 14:48)</a>:</h4>
<p>sadly, safe Rust != Rust without <code>unsafe</code></p>



<a name="180366221"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Extern%20static%20references/near/180366221" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Extern.20static.20references.html#180366221">(Nov 10 2019 at 14:48)</a>:</h4>
<p><code>extern "C" { ... }</code> declarations are unsound, there are a couple of issues regarding that</p>



<a name="180366229"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Extern%20static%20references/near/180366229" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Daniel Henry-Mantilla <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Extern.20static.20references.html#180366229">(Nov 10 2019 at 14:48)</a>:</h4>
<p>That's a bummer.<br>
Could <code>extern static</code> of type <code>T</code> be considered as <code>MaybeUninit&lt;T&gt;</code> until an <code>unsafe</code> access takes place, which under the hoods <code>assume_init</code>s it, hence the <code>unsafe</code>ty ?</p>



<a name="180366242"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Extern%20static%20references/near/180366242" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Extern.20static.20references.html#180366242">(Nov 10 2019 at 14:49)</a>:</h4>
<p><a href="https://github.com/rust-lang/unsafe-code-guidelines/issues/198" target="_blank" title="https://github.com/rust-lang/unsafe-code-guidelines/issues/198">https://github.com/rust-lang/unsafe-code-guidelines/issues/198</a></p>



<a name="180366257"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Extern%20static%20references/near/180366257" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Extern.20static.20references.html#180366257">(Nov 10 2019 at 14:49)</a>:</h4>
<div class="codehilite"><pre><span></span><span class="k">mod</span> <span class="nn">a</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="k">extern</span><span class="w"> </span><span class="s">&quot;C&quot;</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="k">fn</span> <span class="nf">foo</span><span class="p">()</span><span class="w"> </span>-&gt; <span class="kp">&amp;</span><span class="nb">&#39;static</span><span class="w"> </span><span class="k">mut</span><span class="w"> </span><span class="n">T</span><span class="p">;</span><span class="w"></span>
<span class="p">}}</span><span class="w"></span>
<span class="k">mod</span> <span class="nn">b</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="k">extern</span><span class="w"> </span><span class="s">&quot;C&quot;</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="k">fn</span> <span class="nf">foo</span><span class="p">()</span><span class="w"> </span>-&gt; <span class="o">*</span><span class="k">mut</span><span class="w"> </span><span class="n">T</span><span class="p">;</span><span class="w"></span>
<span class="p">}}</span><span class="w"></span>
</pre></div>



<a name="180366331"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Extern%20static%20references/near/180366331" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Extern.20static.20references.html#180366331">(Nov 10 2019 at 14:50)</a>:</h4>
<p>There I am providing two incompatible declarations for the <code>foo</code> symbol</p>



<a name="180366370"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Extern%20static%20references/near/180366370" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Extern.20static.20references.html#180366370">(Nov 10 2019 at 14:51)</a>:</h4>
<p>and <code>b::foo</code> is currently optimized to have the same return type "properties" as <code>a::foo</code></p>



<a name="180366446"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Extern%20static%20references/near/180366446" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Extern.20static.20references.html#180366446">(Nov 10 2019 at 14:53)</a>:</h4>
<p>You can cause the same issue with statics, e.g.,</p>
<div class="codehilite"><pre><span></span><span class="k">mod</span> <span class="nn">a</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="k">extern</span><span class="w"> </span><span class="s">&quot;C&quot;</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="k">static</span><span class="w"> </span><span class="n">V</span>: <span class="kp">&amp;</span><span class="nb">&#39;static</span><span class="w"> </span><span class="n">T</span><span class="p">;</span><span class="w"> </span><span class="p">}</span><span class="w"> </span><span class="p">}</span><span class="w"></span>
<span class="k">mod</span> <span class="nn">b</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="k">extern</span><span class="w"> </span><span class="s">&quot;C&quot;</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="k">static</span><span class="w"> </span><span class="n">V</span>: <span class="o">*</span><span class="k">const</span><span class="w"> </span><span class="n">T</span><span class="p">;</span><span class="w"> </span><span class="p">}</span><span class="w"> </span><span class="p">}</span><span class="w"></span>
</pre></div>


<p>where if you access <code>b::V</code> it will be nonnull, dereferenceable, etc. because it gets optimized as a <code>&amp;T</code></p>



<a name="180366493"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Extern%20static%20references/near/180366493" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Extern.20static.20references.html#180366493">(Nov 10 2019 at 14:54)</a>:</h4>
<p>even if you never access <code>a::V</code>, accessing <code>b::V</code> with the pointer being null will trigger a miscompilation</p>



<a name="180366521"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Extern%20static%20references/near/180366521" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Daniel Henry-Mantilla <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Extern.20static.20references.html#180366521">(Nov 10 2019 at 14:54)</a>:</h4>
<p>We can see <code>extern fn</code> to be a <code>static</code> of the <code>fn(...)</code> / <code>code&lt;ABI&gt;</code> DST type.<br>
Then under my suggestion, these static should all be considered <code>MaybeUninit</code> and only <code>assume_init</code>ed on the <code>unsafe</code> access.<br>
In other words, there are no validity invariants applicable on <code>extern</code> declarations until they get used</p>



<a name="180366533"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Extern%20static%20references/near/180366533" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Extern.20static.20references.html#180366533">(Nov 10 2019 at 14:55)</a>:</h4>
<blockquote>
<p>Could extern static of type T be considered as MaybeUninit&lt;T&gt; until an unsafe access takes place, which under the hoods assume_inits it, hence the unsafety ?</p>
</blockquote>
<p><span class="user-mention" data-user-id="232018">@Daniel Henry-Mantilla</span> maybe? I think the simplest thing would be to require <code>unsafe extern "C" { ... }</code> when writing declarations.</p>



<a name="180366549"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Extern%20static%20references/near/180366549" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Daniel Henry-Mantilla <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Extern.20static.20references.html#180366549">(Nov 10 2019 at 14:55)</a>:</h4>
<p>Yes, that's a far simpler solution. Could that be done in a future edition ?</p>



<a name="180366603"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Extern%20static%20references/near/180366603" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Extern.20static.20references.html#180366603">(Nov 10 2019 at 14:56)</a>:</h4>
<p>It's a syntactic change, so I guess so. It's more of a question of whether the churn will be worth it, and whether there are better ways to fix this issue.</p>



<a name="180366622"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Extern%20static%20references/near/180366622" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Extern.20static.20references.html#180366622">(Nov 10 2019 at 14:57)</a>:</h4>
<p>It's not just extern <code>static</code>s, but extern fn declarations have the same problem.</p>



<a name="180366681"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Extern%20static%20references/near/180366681" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Extern.20static.20references.html#180366681">(Nov 10 2019 at 14:58)</a>:</h4>
<p><a href="https://github.com/rust-lang/unsafe-code-guidelines/issues/84" target="_blank" title="https://github.com/rust-lang/unsafe-code-guidelines/issues/84">https://github.com/rust-lang/unsafe-code-guidelines/issues/84</a></p>



<a name="180367109"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Extern%20static%20references/near/180367109" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Daniel Henry-Mantilla <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Extern.20static.20references.html#180367109">(Nov 10 2019 at 15:09)</a>:</h4>
<p><code>extern fn</code>s could be seen as a very special cased <code>extern static</code>, in the same fashion as a <code>fn</code> "pointer" could be seen as a <code>&amp;'static code&lt;Abi&gt;</code> reference</p>



<a name="180367113"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Extern%20static%20references/near/180367113" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Extern.20static.20references.html#180367113">(Nov 10 2019 at 15:09)</a>:</h4>
<p>I've posted and linked this thread here: <a href="https://github.com/rust-lang/unsafe-code-guidelines/issues/198#issuecomment-552203032" target="_blank" title="https://github.com/rust-lang/unsafe-code-guidelines/issues/198#issuecomment-552203032">https://github.com/rust-lang/unsafe-code-guidelines/issues/198#issuecomment-552203032</a></p>



<a name="180367165"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Extern%20static%20references/near/180367165" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Extern.20static.20references.html#180367165">(Nov 10 2019 at 15:10)</a>:</h4>
<p>There are maybe two things that are UB: one is the violation of the validity invariant by writing null to  <code>FOO</code>. The other one is that for that to happen probably an incompatible definition of <code>FOO</code> exists somewhere.</p>



<a name="180372523"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Extern%20static%20references/near/180372523" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> DutchGhost <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Extern.20static.20references.html#180372523">(Nov 10 2019 at 17:47)</a>:</h4>
<blockquote>
<p>that's instant UB, and you don't need <code>#[no_mangle]</code> for it</p>
<div class="codehilite"><pre><span></span><span class="k">extern</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="k">static</span><span class="w"> </span><span class="n">V</span>: <span class="kp">&amp;</span><span class="nb">&#39;static</span><span class="w"> </span><span class="kt">u8</span><span class="p">;</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
<span class="k">fn</span> <span class="nf">main</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="p">}</span><span class="w"></span>
</pre></div>


</blockquote>
<p>Using <code>V</code> there will cause a linker error, so thats what the</p>
<div class="codehilite"><pre><span></span><span class="k">mod</span> <span class="nn">internal</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="cp">#[no_mangle]</span><span class="w"></span>
<span class="w">    </span><span class="k">static</span><span class="w"> </span><span class="n">V</span>: <span class="kt">usize</span> <span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</pre></div>


<p>was for. And that also forces the external static <code>V</code> to be 0 as far as I know.</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>